﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tunnel.DevicesControls
{
    public partial class BackPanel : Panel
    {
        public BackPanel()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            //this.HorizontalScroll.Visible = true;
            //this.VerticalScroll.Visible = true;

        }
        private Point rectangleTopLeft = new Point(0, 0);
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Graphics dc = pe.Graphics;
            Bitmap bm = new Bitmap(@"PicRes\Road.jpg");
            bm.SetResolution(72, 72);
            Size scrolloffset = new Size(this.AutoScrollPosition);
            dc.DrawImage(bm, rectangleTopLeft + scrolloffset);
            bm.Dispose();

        }

        public int mouseX;
        public int mouseY;
        public int x;
        public int y;

        public bool isMouseDown = false;
        public Point mousePos;
        public Point mouseNow;


        private void PanelEx_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown = true;
            this.AutoScrollPosition = mouseNow;
            this.mousePos = new Point(e.X, e.Y);
            this.Focus();
            //this.AutoScrollPosition = new Point(mousePos.X - e.X, mousePos.Y - e.Y);
        }

        private void PanelEx_MouseMove(object sender, MouseEventArgs e)
        {

            if (isMouseDown)
            {

                mouseX = e.X;
                mouseY = e.Y;
                x = mousePos.X - e.X;

                y = mousePos.Y - e.Y;
                Point p = new Point(mouseNow.X + x, mouseNow.Y + y);
                this.AutoScrollPosition = p;
            }

        }

        private void PanelEx_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
            mouseNow = new Point(-this.AutoScrollPosition.X, -this.AutoScrollPosition.Y);
        }
    }
}
